package mta.localization;

import mta.io.LocalizationPackageFilesIF;
import mta.io.csv.reader.CsvTranslationReader;
import mta.io.csv.reader.CsvTranslationReaderFactoryIF;

import java.util.HashMap;

/**
 * Created by Dmitry Nikolaev.
 * User: dima
 * Date: 19.01.2009
 * Time: 19:15:14
 */
public class LocalizationPackageBuilder {
    private LocalizationPackageFilesIF lpFiles;
    private CsvTranslationReaderFactoryIF readerFactory;

    public LocalizationPackageBuilder(LocalizationPackageFilesIF lpFiles, CsvTranslationReaderFactoryIF readerFactory) {
        this.lpFiles = lpFiles;
        this.readerFactory = readerFactory;
    }

    public LocalizationPackage buildLocalizationFactory() throws Exception {
        LocalizationPackage resultPackage = new LocalizationPackage();

        for (String file : lpFiles.listFilenames()) {
            CsvTranslationReader reader = readerFactory.buildReader(lpFiles.getFullName(file));
            ReadCsvIntoTranslation(resultPackage, file, reader);
        }
        return resultPackage;
    }

    private void ReadCsvIntoTranslation(LocalizationPackage resultPackage, String file, CsvTranslationReader reader) {
        HashMap<String, String> filePhrases = reader.getPhrases();
        for (String original : filePhrases.keySet()) {
            String translation = filePhrases.get(original);
            resultPackage.setTranslation(file, original, translation);
        }
    }
}
